package com.example.datademo;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    //姓名输入框
    private EditText etName;
    //性别输入框
    private EditText etGender;
    //年龄输入框
    private EditText etAge;
    //编号输入框
    private EditText etId;
    //添加数据按钮
    private Button btnInsert;
    //删除数据按钮
    private Button btnDelete;
    //修改数据按钮
    private Button btnModify;
    //查询数据按钮
    private Button btnQuery;
    //学生信息列表
    private ListView lvStuInfo;
    private ContentResolver mResover;
    private Uri mUri;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        etName = findViewById(R.id.et_name_activity_sqlite);
        etGender = findViewById(R.id.et_gender_activity_sqlite);
        etAge = findViewById(R.id.et_age_activity_sqlite);
        etId = findViewById(R.id.et_id_activity_sqlite);
        btnInsert = findViewById(R.id.btn_insert_activity_sqlite);
        btnDelete = findViewById(R.id.btn_delete_activity_sqlite);
        btnModify = findViewById(R.id.btn_modify_activity_sqlite);
        btnQuery = findViewById(R.id.btn_query_activity_sqlite);
        lvStuInfo = findViewById(R.id.lv_student_info_activity_sqlite);

    }

    private void initData() {
        mResover = getContentResolver();
        mUri = Uri.parse("content://com.yinggu.Newpractise");

    }

    private void initEvent() {
        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues values = new ContentValues();
                values.put("name",etName.getText().toString());
                values.put("age",etAge.getText().toString());
                values.put("gender",etGender.getText().toString());
                Uri insert = mResover.insert(mUri, values);
                long id = ContentUris.parseId(insert);
                Toast.makeText(MainActivity.this, "新同学id：" + id, Toast.LENGTH_LONG).show();

            }
        });
        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor cursor = mResover.query(mUri, null, null, null, null);
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                        MainActivity.this,R.layout.item_activity_sqlite,cursor,
                        new String[]{"_id","name","age","gender"},
                        new  int[] { R.id.id_item_activity_sqlite,
                                      R.id.name_item_activity_sqlite,
                                      R.id.age_item_activity_sqlite,
                                      R.id.gender_item_activity_sqlite});
                                      lvStuInfo.setAdapter(adapter);
            }
        });
        btnModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ContentValues values = new ContentValues();
                values.put("name",etName.getText().toString());
                values.put("age",etAge.getText().toString());
                values.put("gender",etGender.getText().toString());

                int result = mResover.update(mUri, values, "_id = ?", new String[]{etId.getText().toString()});
                if (result > 0) {
                    Toast.makeText(MainActivity.this,"修改成功"+result,Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this,"修改失败"+result,Toast.LENGTH_LONG).show();
                }
            }
        });
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

/*                int result = mResover.delete(mUri, "_id = ?", new String[]{etId.getText().toString()});

                if (result > 0) {
                    Toast.makeText(MainActivity.this,"修改成功"+result,Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this,"修改失败"+result,Toast.LENGTH_LONG).show();
                }*/

                mResover.delete(
                        Uri.parse("content://com.yinggu.Newpractise/helloworld"),
                        null,
                        null);
                mResover.delete(
                        Uri.parse("content://com.yinggu.Newpractise/helloworld/abc"),
                        null,
                        null);
                mResover.delete(
                        Uri.parse("content://com.yinggu.Newpractise/helloworld/123"),
                        null,
                        null);
                mResover.delete(
                        Uri.parse("content://com.yinggu.Newpractise/helloworld/abc123"),
                        null,
                        null);
            }
        });


    }
}
